﻿using Barcode_Project.Entity;
using Barcode_Project.Local;
using Barcode_Project.Network;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Barcode_Project.ProBarPrint
{
    public partial class UserSelector : DevExpress.XtraEditors.XtraForm
    {
        public UserSelectItem selectedUser { get; set; }

        public string gxCode { get; set; }

        public UserSelector(string inputGxCode)
        {
            InitializeComponent();

            gxCode = inputGxCode;

            SearchUser();
        }

        private void btn_search_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SearchUser();
        }
        void SearchUser()
        {
            var dic = new Dictionary<string, string>();
            CommonMethods.SetParameterValue(ref dic, group_data.Controls, "input_");

            dic["gxCode"] = gxCode;

            var data = new RemoteRequest().SearchUsers(dic);

            gridControl_users.DataSource = data;
            gridControl_users.RefreshDataSource();
        }

        private void btn_confirm_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult = DialogResult.OK;

            var gridView = gridControl_users.FocusedView as GridView;
            var item = (UserSelectItem)gridView_users.GetRow(gridView.FocusedRowHandle);

            selectedUser = new UserSelectItem
            {
                userCode = item.userCode,
                userName = item.userName,
            };

            Close();
        }

        private void btn_close_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void gridView_users_DoubleClick(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;

            var gridView = gridControl_users.FocusedView as GridView;
            var item = (UserSelectItem)gridView_users.GetRow(gridView.FocusedRowHandle);

            selectedUser = new UserSelectItem
            {
                userCode = item.userCode,
                userName = item.userName,
            };

            Close();
        }
    }
}